/**
 * @file userTime.h
 * @brief
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-03-01
 *
 * @copyright Copyright (c) 2024  by xuesong
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-03-01     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */

#include "stdint.h"
#include <stdbool.h>

#ifndef _USER_TIME_H_
#define _USER_TIME_H_

#include "stdio.h"

struct ustm
{
    int8_t tm_sec;   /* seconds after the minute, 0 to 60 (0 - 60 allows for the occasional leap second) */
    int8_t tm_min;   /* minutes after the hour, 0 to 59 */
    int8_t tm_hour;  /* hours since midnight, 0 to 23 */
    int8_t tm_mday;  /* day of the month, 1 to 31 */
    int8_t tm_mon;   /* months since January, 0 to 11 */
    int16_t tm_year; /* years since 1900 */
    int8_t tm_wday;  /* days since Sunday, 0 to 6 */
    int8_t tm_yday;  /* days since January 1, 0 to 365 */
    int8_t tm_isdst; /* Daylight Savings Time flag */
};

int32_t user_mktime(struct ustm *pT);
void user_localtime(struct ustm *pT, int32_t tim);

#define RTC_YEAR_SIZE (sizeof(u16))
#define RTC_NO_YEAR_SIZE (sizeof(u8))
#define RTC_TOTALSECOND_SIZE (sizeof(u32))
#define USED_INTERNAL_32KRC_CAL 1
#define USED_TOTAL_SECOND_CAL_SYS_TIME 1

#if USED_TOTAL_SECOND_CAL_SYS_TIME
// #include "base_u64Operation.h"
#endif

typedef enum
{
    NONLEAP_YEAR = 0,
    LEAP_YEAR = 1,
} FIND_LEAP_YEAR;

#if USED_TOTAL_SECOND_CAL_SYS_TIME
typedef enum
{
    TOTAL_SECOND_FOR_BASE_YEAR = 1262275200, /// reference by base year
    TOTAL_SECOND_NONLEAP_YEAR = 31536000,
    TOTAL_SECOND_LEAP_YEAR = 31622400,
    TOTAL_SECOND_PER__WEEK = 604800,
    TOTAL_SECOND_PER_DAY = 86400,
    TOTAL_SECOND_PER_HOUR = 3600,
    TOTAL_SECOND_PER_MIN = 60,
} TOTAL_SECOND;

typedef enum
{
    TOTAL_SECOND_BASE_TIME_ERR = 0xfffffffe,
    TOTAL_SECOND_NOW_TIME_ERR = 0xfffffffc,
    TOTAL_SECOND_TIME_ERR = 0xfffffff8,
} TOTAL_SECOND_ERR;

#endif
typedef enum
{
    WEEK_MONDAY = 0,
    WEEK_TUESDAY = 1,
    WEEK_WEDNESDAY = 2,
    WEEK_THURSDAY = 3,
    WEEK_FRIDAY = 4,
    WEEK_SATURDAY = 5,
    WEEK_SUNDAY = 6,
} WEEK_DAY;

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef struct
{
    u16 year;
    u8 month;
    u8 day;

    u8 hour;
    u8 min;
    u8 second;
    u8 week;

    u64 totalSecond;
    u64 time_timeZone_s;
} rtc_format_t;

typedef union
{
    struct
    {
        unsigned char _16MS : 1;      //
        unsigned char _32MS : 1;      //
        unsigned char _64MS : 1;      //
        unsigned char _128MS : 1;     //
        unsigned char _256MS : 1;     //
        unsigned char HalfSecond : 1; //
        unsigned char Second : 1;     //
    } Bits;
    uint8_t Byte;
} ClockFlag_t;
void testClock(void);

#endif
